package com.youyi.yychosesdk.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.lkx.liandianqinew.R;
import com.youyi.yychosesdk.constant.Type;
import com.youyi.yychosesdk.models.album.entity.Photo;
import com.youyi.yychosesdk.result.Result;
import com.youyi.yychosesdk.setting.Setting;
import com.youyi.yychosesdk.ui.widget.PressedImageView;
import com.youyi.yychosesdk.utils.media.DurationUtils;
import java.util.ArrayList;

public class PhotosAdapter extends RecyclerView.Adapter {
    private static final int TYPE_ALBUM_ITEMS = 2;
    private static final int TYPE_CAMERA = 1;
    private ArrayList<Object> dataList;
    private boolean isSingle;
    private OnClickListener listener;
    private LayoutInflater mInflater;
    private int singlePosition;
    private boolean unable;

    public interface OnClickListener {
        void onCameraClick();

        void onPhotoClick(int i, int i2);

        void onSelectorChanged();

        void onSelectorOutOfMax(Integer num);
    }

    public PhotosAdapter(Context context, ArrayList<Object> arrayList, OnClickListener onClickListener) {
        this.dataList = arrayList;
        this.listener = onClickListener;
        this.mInflater = LayoutInflater.from(context);
        boolean z = false;
        this.unable = Result.count() == Setting.count;
        this.isSingle = Setting.count == 1 ? true : z;
    }

    public void change() {
        this.unable = Result.count() == Setting.count;
        notifyDataSetChanged();
    }

    @Override // android.support.v7.widget.RecyclerView.Adapter
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        if (i != 1) {
            return new PhotoViewHolder(this.mInflater.inflate(R.layout.item_rv_photos_easy_photos, viewGroup, false));
        }
        return new CameraViewHolder(this.mInflater.inflate(R.layout.item_camera_easy_photos, viewGroup, false));
    }

    @Override // android.support.v7.widget.RecyclerView.Adapter
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int a) {
        if (viewHolder instanceof PhotoViewHolder) {
            final Photo photo = (Photo) this.dataList.get(a);
            if (photo != null) {
                PhotoViewHolder photoViewHolder = (PhotoViewHolder) viewHolder;
                updateSelector(photoViewHolder.tvSelector, photo.selected, photo, a);
                String str = photo.path;
                Uri uri = photo.uri;
                String str2 = photo.type;
                long j = photo.duration;
                boolean z = str.endsWith(Type.GIF) || str2.endsWith(Type.GIF);
                if (Setting.showGif && z) {
                    Setting.imageEngine.loadGifAsBitmap(photoViewHolder.ivPhoto.getContext(), uri, photoViewHolder.ivPhoto);
                    photoViewHolder.tvType.setText(R.string.gif_easy_photos);
                    photoViewHolder.tvType.setVisibility(View.VISIBLE);
                    photoViewHolder.ivVideo.setVisibility(View.GONE);
                } else if (!Setting.showVideo || !str2.contains(Type.VIDEO)) {
                    Setting.imageEngine.loadPhoto(photoViewHolder.ivPhoto.getContext(), uri, photoViewHolder.ivPhoto);
                    photoViewHolder.tvType.setVisibility(View.GONE);
                    photoViewHolder.ivVideo.setVisibility(View.GONE);
                } else {
                    Setting.imageEngine.loadPhoto(photoViewHolder.ivPhoto.getContext(), uri, photoViewHolder.ivPhoto);
                    photoViewHolder.tvType.setText(DurationUtils.format(j));
                    photoViewHolder.tvType.setVisibility(View.VISIBLE);
                    photoViewHolder.ivVideo.setVisibility(View.VISIBLE);
                }
                photoViewHolder.vSelector.setVisibility(View.VISIBLE);
                photoViewHolder.tvSelector.setVisibility(View.VISIBLE);
                photoViewHolder.ivPhoto.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View view) {
                        int i = a;
                        if (Setting.isShowCamera && !Setting.isBottomRightCamera()) {
                            i--;
                        }
                        PhotosAdapter.this.listener.onPhotoClick(i, i);
                    }
                });
                photoViewHolder.vSelector.setOnClickListener(new View.OnClickListener() {
                    /* class com.youyi.yychosesdk.ui.adapter.PhotosAdapter.AnonymousClass2 */

                    public void onClick(View view) {
                        if (PhotosAdapter.this.isSingle) {
                            PhotosAdapter.this.singleSelector(photo, a);
                        } else if (!PhotosAdapter.this.unable) {
                            photo.selected = !photo.selected;
                            if (photo.selected) {
                                int addPhoto = Result.addPhoto(photo);
                                if (addPhoto != 0) {
                                    PhotosAdapter.this.listener.onSelectorOutOfMax(Integer.valueOf(addPhoto));
                                    photo.selected = false;
                                    return;
                                }
                                ((PhotoViewHolder) viewHolder).tvSelector.setBackgroundResource(R.drawable.bg_select_true_easy_photos);
                                ((PhotoViewHolder) viewHolder).tvSelector.setText(String.valueOf(Result.count()));
                                if (Result.count() == Setting.count) {
                                    PhotosAdapter.this.unable = true;
                                    PhotosAdapter.this.notifyDataSetChanged();
                                }
                            } else {
                                Result.removePhoto(photo);
                                if (PhotosAdapter.this.unable) {
                                    PhotosAdapter.this.unable = false;
                                }
                                PhotosAdapter.this.notifyDataSetChanged();
                            }
                            PhotosAdapter.this.listener.onSelectorChanged();
                        } else if (photo.selected) {
                            Result.removePhoto(photo);
                            if (PhotosAdapter.this.unable) {
                                PhotosAdapter.this.unable = false;
                            }
                            PhotosAdapter.this.listener.onSelectorChanged();
                            PhotosAdapter.this.notifyDataSetChanged();
                        } else {
                            PhotosAdapter.this.listener.onSelectorOutOfMax(null);
                        }
                    }
                });
            }
        } else if (viewHolder instanceof CameraViewHolder) {
            ((CameraViewHolder) viewHolder).flCamera.setOnClickListener(new View.OnClickListener() {
                /* class com.youyi.yychosesdk.ui.adapter.PhotosAdapter.AnonymousClass3 */

                public void onClick(View view) {
                    PhotosAdapter.this.listener.onCameraClick();
                }
            });
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void singleSelector(Photo photo, int i) {
        if (Result.isEmpty()) {
            Result.addPhoto(photo);
        } else if (Result.getPhotoPath(0).equals(photo.path)) {
            Result.removePhoto(photo);
        } else {
            Result.removePhoto(0);
            Result.addPhoto(photo);
            notifyItemChanged(this.singlePosition);
        }
        notifyItemChanged(i);
        this.listener.onSelectorChanged();
    }

    private void updateSelector(TextView textView, boolean z, Photo photo, int i) {
        if (z) {
            String selectorNumber = Result.getSelectorNumber(photo);
            if (selectorNumber.equals("0")) {
                textView.setBackgroundResource(R.drawable.bg_select_false_easy_photos);
                textView.setText((CharSequence) null);
                return;
            }
            textView.setText(selectorNumber);
            textView.setBackgroundResource(R.drawable.bg_select_true_easy_photos);
            if (this.isSingle) {
                this.singlePosition = i;
                textView.setText("1");
                return;
            }
            return;
        }
        if (this.unable) {
            textView.setBackgroundResource(R.drawable.bg_select_false_unable_easy_photos);
        } else {
            textView.setBackgroundResource(R.drawable.bg_select_false_easy_photos);
        }
        textView.setText((CharSequence) null);
    }

    @Override // android.support.v7.widget.RecyclerView.Adapter
    public int getItemCount() {
        return this.dataList.size();
    }

    @Override // android.support.v7.widget.RecyclerView.Adapter
    public int getItemViewType(int i) {
        return (i != 0 || !Setting.isShowCamera || Setting.isBottomRightCamera()) ? 2 : 1;
    }

    private static class CameraViewHolder extends RecyclerView.ViewHolder {
        final FrameLayout flCamera;

        CameraViewHolder(View view) {
            super(view);
            this.flCamera = (FrameLayout) view.findViewById(R.id.fl_camera);
        }
    }

    public static class PhotoViewHolder extends RecyclerView.ViewHolder {
        final PressedImageView ivPhoto;
        final ImageView ivVideo;
        final TextView tvSelector;
        final TextView tvType;
        final View vSelector;

        PhotoViewHolder(View view) {
            super(view);
            this.ivPhoto = (PressedImageView) view.findViewById(R.id.iv_photo);
            this.tvSelector = (TextView) view.findViewById(R.id.tv_selector);
            this.vSelector = view.findViewById(R.id.v_selector);
            this.tvType = (TextView) view.findViewById(R.id.tv_type);
            this.ivVideo = (ImageView) view.findViewById(R.id.iv_play);
        }
    }
}
